package controllers;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import play.libs.Codec;
import models.SystemOperator;
import models.SystemParams;
import services.OperatorService;
import services.SystemParamService;
import utils.BizConstants;
import utils.Validate;
import vos.ReturnCode;
import vos.ReturnListVo;
import vos.SystemOperatorVo;

import com.github.pagehelper.PageInfo;

/**
 * 操作员管理控制类
 * @author lifengguo
 *
 */
public class OperatorController extends BaseController {
	/**
	 *用户列表
	 */
	public static void operatorlist(){
		render();
	}
	/**
	 * 获取用户列表
	 * @param page
	 * @param rows
	 * @param sort
	 * @param order
	 * @param searchText 搜索条件
	 */
	public static void getList(int page,int rows,String sort,String order,
			String searchText){
		PageInfo<SystemOperatorVo> cases=OperatorService.getList(page,rows,sort,order,searchText);
		renderJSON(new ReturnListVo(cases.getTotal(),cases.getList()));
	}
	/**
	 * 添加用户信息
	 */
	public static void addOperator(){
		//获取最小密级和最大密级的id
		String maxLevel=OperatorService.getMaxLevel();
		renderArgs.put("maxLevel", maxLevel);
		String minLevel=OperatorService.getMinLevel();
		renderArgs.put("minLevel", minLevel);
		render();
	}
	/**
	 * 编辑用户信息
	 */
	public static void editOperator(String operid){
		SystemOperator operator=OperatorService.getById(operid);
		renderArgs.put("operator", operator);
		//获取最小密级和最大密级的id
		String maxLevel=OperatorService.getMaxLevel();
		renderArgs.put("maxLevel", maxLevel);
		String minLevel=OperatorService.getMinLevel();
		renderArgs.put("minLevel", minLevel);
		render();
	}
	/**
	 * 检查用户编号是否已存在
	 */
	public static void checkCode(String value){
		if(StringUtils.isEmpty(value)){
			renderRespondJson(ReturnCode.ErrorCode, "编号不能为空");
		}
		if(value.length()>50){
			renderRespondJson(ReturnCode.ErrorCode, "编号长度超出限制");
		}
		boolean isexist=OperatorService.checkCode(value);
		if(isexist){
			renderRespondJson(ReturnCode.ErrorCode, "编号已存在");
		}
		else{
			renderRespondJson(ReturnCode.SuccessCode, "该编号可以使用");
		}
	}
	/**
	 * 添加操作员
	 * @param operator
	 */
	public static void saveOperator(SystemOperator operator,String confimpassword){
		response.contentType="text/html;charset=utf-8";
		if(operator==null){
			renderRespondJson(ReturnCode.ErrorCode, "请填写完整信息");
		}
		if(StringUtils.isEmpty(operator.getOperatorid())){
			renderRespondJson(ReturnCode.ErrorCode, "用户编号不可为空");
		}
		if(StringUtils.isEmpty(operator.getOperatorname())){
			operator.setOperatorname(operator.getOperatorid());
		}
		if(StringUtils.isNotEmpty(operator.getPhone())&&!Validate.isMobile(operator.getPhone())){
			renderRespondJson(ReturnCode.ErrorCode, "手机号格式不正确");
		}
		if(StringUtils.isNotEmpty(operator.getEmail())&&!Validate.isEmail(operator.getEmail())){
			renderRespondJson(ReturnCode.ErrorCode, "邮箱格式不正确");
		}
		if(StringUtils.isEmpty(operator.getPassword())){
			renderRespondJson(ReturnCode.ErrorCode, "用户密码不可为空");
		}
		if(operator.getPassword().length()<4){
			renderRespondJson(ReturnCode.ErrorCode, "密码长度不可小于4");
		}
		if(StringUtils.isEmpty(confimpassword)){
			renderRespondJson(ReturnCode.ErrorCode, "确认密码不可为空");
		}
		if(!confimpassword.equals(operator.getPassword())){
			renderRespondJson(ReturnCode.ErrorCode, "两次密码输入不一致");
		}
		if(StringUtils.isEmpty(operator.getRoleId())){
			renderRespondJson(ReturnCode.ErrorCode, "角色不可为空");
		}
		if(StringUtils.isEmpty(operator.getSecretLevel())){
			renderRespondJson(ReturnCode.ErrorCode, "密级不可为空");
		}
		String ip=request.remoteAddress;
		boolean isSave=OperatorService.saveOperator(operator,getLoginUser(),ip);
		if(isSave){
			renderRespondJson(ReturnCode.SuccessCode, "保存成功");
		}
		renderRespondJson(ReturnCode.ErrorCode, "服务器开小差了~");
	}
	/**
	 * 批量停用
	 * @param ids 操作员编号
	 */
	public static void betchDisable(String ids){
		if(StringUtils.isEmpty(ids)){
			renderRespondJson(ReturnCode.ErrorCode, "请选择要停用的操作员");
		}
		String[] oprIds=ids.split(",");
		List<String> listIds=new ArrayList<String>();
		Collections.addAll(listIds,oprIds);
		String ip=request.remoteAddress;
		String moudel="批量停用操作员："+ids;
		boolean isdis=OperatorService.betchDisable(listIds,ip,getLoginUser(),moudel,new BigDecimal(2));
		if(isdis){
			renderRespondJson(ReturnCode.SuccessCode, "停用成功");
		}
		renderRespondJson(ReturnCode.ErrorCode, "服务器开小差了~");
	}
	/**
	 * 批量启用
	 * @param ids 操作员编号
	 */
	public static void betchEnable(String ids){
		if(StringUtils.isEmpty(ids)){
			renderRespondJson(ReturnCode.ErrorCode, "请选择要启用的操作员");
		}
		String[] oprIds=ids.split(",");
		List<String> listIds=new ArrayList<String>();
		Collections.addAll(listIds,oprIds);
		String ip=request.remoteAddress;
		String moudel="批量启用操作员："+ids;
		boolean isdis=OperatorService.betchDisable(listIds,ip,getLoginUser(),moudel,new BigDecimal(1));
		if(isdis){
			renderRespondJson(ReturnCode.SuccessCode, "启用成功");
		}
		renderRespondJson(ReturnCode.ErrorCode, "服务器开小差了~");
	}
	/**
	 * 添加操作员
	 * @param operator
	 */
	public static void updateOperator(SystemOperator operator){
		response.contentType="text/html;charset=utf-8";
		if(operator==null){
			renderRespondJson(ReturnCode.ErrorCode, "请填写完整信息");
		}
		if(StringUtils.isEmpty(operator.getOperatorname())){
			operator.setOperatorname(operator.getOperatorid());
		}
		if(StringUtils.isEmpty(operator.getRoleId())){
			renderRespondJson(ReturnCode.ErrorCode, "角色不可为空");
		}
		if(StringUtils.isNotEmpty(operator.getPhone())&&!Validate.isMobile(operator.getPhone())){
			renderRespondJson(ReturnCode.ErrorCode, "手机号格式不正确");
		}
		if(StringUtils.isNotEmpty(operator.getEmail())&&!Validate.isEmail(operator.getEmail())){
			renderRespondJson(ReturnCode.ErrorCode, "邮箱格式不正确");
		}
		if(StringUtils.isEmpty(operator.getSecretLevel())){
			renderRespondJson(ReturnCode.ErrorCode, "密级不可为空");
		}
		String ip=request.remoteAddress;
		boolean isSave=OperatorService.updateOperator(operator,getLoginUser(),ip);
		if(isSave){
			renderRespondJson(ReturnCode.SuccessCode, "保存成功");
		}
		renderRespondJson(ReturnCode.ErrorCode, "服务器开小差了~");
	}
	/**
	 * 重置密码
	 * @param operid 操作员id
	 */
	public static void resetPass(String operid){
		renderArgs.put("operid", operid);
		render();
	}
	
	/**
	 * 重置密码
	 */
	public static void resetPassword(String operatorid,String password,String confimpassword){
		if(StringUtils.isEmpty(operatorid)){
			renderRespondJson(ReturnCode.ErrorCode, "请选择操作员");
		}
		if(StringUtils.isEmpty(confimpassword)||StringUtils.isEmpty(password)){
			renderRespondJson(ReturnCode.ErrorCode, "请输入密码");
		}
		if(password.length()<4){
			renderRespondJson(ReturnCode.ErrorCode, "密码长度不可小于4");
		}
		if(!confimpassword.equals(password)){
			renderRespondJson(ReturnCode.ErrorCode, "两次输入的密码不一致");
		}
//		String param=SystemParamService.getValueByCacheId(BizConstants.PARAM_MAP.get("RESET_PASSWORD_VALUE"));
		SystemOperator oper=getLoginUser();
		SystemOperator oldOper=OperatorService.getById(operatorid);
		String pass=Codec.hexMD5(password+oldOper.getSalt());
		String ip=request.remoteAddress;
		boolean isok=OperatorService.updatePass(operatorid,pass,oper,ip);
		if(isok){
			renderRespondJson(ReturnCode.SuccessCode,"重置成功");
		}
		else{
			renderRespondJson(ReturnCode.ErrorCode,"重置失败");
		}
	}
}
